/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package se.sll.engagemangsindex.ejb;

import java.util.ArrayList;
import java.util.List;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author khaleddaham
 */
@Singleton
@Startup
public class LogicalAddressCache {

    private static final Logger LOG = (Logger) LoggerFactory.getLogger(LogicalAddressCache.class);
    private List<String> addressCache = null;

    /**
     *
     * @param addressList
     */
    public void updateCache(List<String> addressList) {
        synchronized (this) {
            if (addressList != null) {
                LOG.info("Adding {} addresses to the cache.", addressList.size());
                reset();
                addressCache = addressList;
            }
        }
    }

    /**
     *
     */
    protected void reset() {
        addressCache = null;
    }

    /**
     *
     * @return
     */
    public List<String> getLogicalAddresses() {
        List<String> addressList = new ArrayList<String>();
        synchronized (this) {
            if (addressCache != null) {
                addressList.addAll(this.addressCache);
            }
        }
        return addressList;
    }
}